package cn.jietuo.first.cartoonalgorithm;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/30
 * @description: 自定义一个数组，满足插入功能
 */
public class MyArray {

    private int size;

    private int[] array;

    public MyArray(int capacity) {
        array = new int[capacity];
        size = 0;
    }

    /**
     * 数组插入
     */
    public void insert(int element, int index) {
        if (index < 0 || index > size){
            throw new IndexOutOfBoundsException("数组角标越界");
        }

        // 从右向左循环，将元素逐个向右挪一位
        for (int i = size -1; i >= i; i--) {
            array[i+1] = array[index];
        }

        // 腾出位置放入新元素
        array[index] = element;
        size ++;
    }

    public void printArray(){
        for (int i = 0; i < size; i++) {
            System.out.println(array[i]);
        }
    }

    public static void main(String[] args) {
        MyArray myArray = new MyArray(10);
        myArray.insert(3,0);
        myArray.printArray();
    }

}
